﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using MySql.Data.MySqlClient;

namespace DAL
{
    public class DTaxiArea
    {
        #region  Method

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("id", "ttaxiarea");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int taxiid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ttaxiarea");
            strSql.Append(" where Taxiid=@Taxiid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Taxiid", MySqlDbType.Int32)
			};
            parameters[0].Value = taxiid;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.TaxiAreaInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ttaxiarea(");
            strSql.Append("Taxiid,LeftLat,LeftLon,RightLat,RightLon,STime,ETime)");
            strSql.Append(" values (");
            strSql.Append("@Taxiid,@LeftLat,@LeftLon,@RightLat,@RightLon,@STime,@ETime)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Taxiid", MySqlDbType.Int32,10),
					new MySqlParameter("@LeftLat", MySqlDbType.Double),
					new MySqlParameter("@LeftLon", MySqlDbType.Double),
					new MySqlParameter("@RightLat", MySqlDbType.Double),
					new MySqlParameter("@RightLon", MySqlDbType.Double),
					new MySqlParameter("@STime", MySqlDbType.VarChar,20),
					new MySqlParameter("@ETime", MySqlDbType.VarChar,20)};
            parameters[0].Value = model.Taxiid;
            parameters[1].Value = model.LeftLat;
            parameters[2].Value = model.LeftLon;
            parameters[3].Value = model.RightLat;
            parameters[4].Value = model.RightLon;
            parameters[5].Value = model.STime;
            parameters[6].Value = model.ETime;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.TaxiAreaInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ttaxiarea set ");
            strSql.Append("Taxiid=@Taxiid,");
            strSql.Append("LeftLat=@LeftLat,");
            strSql.Append("LeftLon=@LeftLon,");
            strSql.Append("RightLat=@RightLat,");
            strSql.Append("RightLon=@RightLon,");
            strSql.Append("STime=@STime,");
            strSql.Append("ETime=@ETime");
            strSql.Append(" where Taxiid=@Taxiid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Taxiid", MySqlDbType.Int32,10),
					new MySqlParameter("@LeftLat", MySqlDbType.Double),
					new MySqlParameter("@LeftLon", MySqlDbType.Double),
					new MySqlParameter("@RightLat", MySqlDbType.Double),
					new MySqlParameter("@RightLon", MySqlDbType.Double),
					new MySqlParameter("@STime", MySqlDbType.VarChar,20),
					new MySqlParameter("@ETime", MySqlDbType.VarChar,20)};
            parameters[0].Value = model.Taxiid;
            parameters[1].Value = model.LeftLat;
            parameters[2].Value = model.LeftLon;
            parameters[3].Value = model.RightLat;
            parameters[4].Value = model.RightLon;
            parameters[5].Value = model.STime;
            parameters[6].Value = model.ETime;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int taxiid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ttaxiarea ");
            strSql.Append(" where Taxiid=@Taxiid");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Taxiid", MySqlDbType.Int32)
			};
            parameters[0].Value = taxiid;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ttaxiarea ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.TaxiAreaInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,Taxiid,LeftLat,LeftLon,RightLat,RightLon,STime,ETime from ttaxiarea ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
			};
            parameters[0].Value = id;

            Model.TaxiAreaInfo model = new Model.TaxiAreaInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Taxiid"] != null && ds.Tables[0].Rows[0]["Taxiid"].ToString() != "")
                {
                    model.Taxiid = int.Parse(ds.Tables[0].Rows[0]["Taxiid"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LeftLat"] != null && ds.Tables[0].Rows[0]["LeftLat"].ToString() != "")
                {
                    //model.LeftLat=ds.Tables[0].Rows[0]["LeftLat"].ToString();
                }
                if (ds.Tables[0].Rows[0]["LeftLon"] != null && ds.Tables[0].Rows[0]["LeftLon"].ToString() != "")
                {
                    //model.LeftLon=ds.Tables[0].Rows[0]["LeftLon"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RightLat"] != null && ds.Tables[0].Rows[0]["RightLat"].ToString() != "")
                {
                    //model.RightLat=ds.Tables[0].Rows[0]["RightLat"].ToString();
                }
                if (ds.Tables[0].Rows[0]["RightLon"] != null && ds.Tables[0].Rows[0]["RightLon"].ToString() != "")
                {
                    //model.RightLon=ds.Tables[0].Rows[0]["RightLon"].ToString();
                }
                if (ds.Tables[0].Rows[0]["STime"] != null && ds.Tables[0].Rows[0]["STime"].ToString() != "")
                {
                    model.STime = ds.Tables[0].Rows[0]["STime"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ETime"] != null && ds.Tables[0].Rows[0]["ETime"].ToString() != "")
                {
                    model.ETime = ds.Tables[0].Rows[0]["ETime"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,Taxiid,LeftLat,LeftLon,RightLat,RightLon,STime,ETime ");
            strSql.Append(" FROM ttaxiarea ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM ttaxiarea ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from ttaxiarea T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            MySqlParameter[] parameters = {
                    new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
                    new MySqlParameter("@PageSize", MySqlDbType.Int32),
                    new MySqlParameter("@PageIndex", MySqlDbType.Int32),
                    new MySqlParameter("@IsReCount", MySqlDbType.Bit),
                    new MySqlParameter("@OrderType", MySqlDbType.Bit),
                    new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ttaxiarea";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
